class Solution
{
public:
    int countCompleteInner(string s, int k)
    {
        int result = 0;
        for (int charTypeCount = 1; charTypeCount <= 26; charTypeCount++)
        {
            int len = charTypeCount * k;
            if (len > s.size())
            {
                break;
            }

            vector<int> charCount(26, 0);
            for (int i = 0; i < len; i++)
            {
                charCount[s[i] - 'a']++;
            }

            auto isComplete = [&]()
            {
                for (int i = 0; i < 26; i++)
                {
                    if (charCount[i] != 0 && charCount[i] != k)
                    {
                        return false;
                    }
                }
                return true;
            };

            if (isComplete())
            {
                result++;
            }
            for (int i = len; i < s.size(); i++)
            {
                charCount[s[i] - 'a']++;
                charCount[s[i - len] - 'a']--;
                if (isComplete())
                {
                    result++;
                }
            }
        }
        return result;
    }

    int countCompleteSubstrings(string word, int k)
    {
        vector<int> continualSubBegins;
        continualSubBegins.push_back(0);
        int n = word.size();
        for (int i = 1; i < n; i++)
        {
            if (abs(word[i] - word[i - 1]) > 2)
            {
                continualSubBegins.push_back(i);
            }
        }
        continualSubBegins.push_back(n);

        int result = 0;
        for (int i = 0; i < continualSubBegins.size() - 1; i++)
        {
            result += countCompleteInner(word.substr(continualSubBegins[i], continualSubBegins[i + 1] - continualSubBegins[i]), k);
        }
        return result;
    }
};